<?php

/**
 * stats actions.
 *
 * @package    bgMicroLeague
 * @subpackage stats
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class statsActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->filter = new GameResultForm($this->getFilters());

    if($this->getUser()->isAuthenticated())
    {
      $this->filter->setOption('user', $this->getUser());
    }
  }  

  public function executeFilter(sfWebRequest $request)
  {    
    $this->filter = new GameResultForm($this->getFilters());

    if($this->getUser()->isAuthenticated())
    {
      $this->filter->setOption('user', $this->getUser());
    }
    
    $this->filter->bind($request->getParameter($this->filter->getName()));

    if ($this->filter->isValid())
    {
      $this->setFilters($this->filter->getValues());

      $this->redirect('@stats_league');
    }

    $this->setTemplate('index');
  }
  
  public function executeLeague(sfWebRequest $request)
  {      
    $this->setFilterOrRedirect();
    
    $this->setLeagueOr404();
    
    $this->players = PlayerQuery::create()->
                        getLeagueSummaryCriteria($this->league, $this->filter)->
                        paginate($request->getParameter("page"), sfConfig::get('max_match_per_page', 10));

  }

  public function executePlayer(sfWebRequest $request)
  {
    $this->setFilterOrRedirect();
    
    $this->setPlayerOr404($request);

    $this->setLeagueOr404();

    //REFACTORING
    
    $this->boardgames = BoardgameQuery::create()->
            getPlayerSummaryCriteria($this->league, $this->player, $this->filter)->
            paginate($request->getParameter("page"), sfConfig::get('max_match_per_page', 10));

  }

  public function executeBoardgames(sfWebRequest $request)
  {
    $this->setLeagueOr404($request);

    $this->boardgames = BoardgameQuery::create()->
                          getByLeagueWithGameCountAndPtsSumCriteria($this->league)->
                          paginate($request->getParameter("page"), sfConfig::get('max_match_per_page', 10));
  }

  protected function getFilters()
  {
    return $this->getUser()->getAttribute('stats.filters', array(), sfConfig::get('app_application_namespace'));
  }

  protected function setFilters(array $filters)
  {
    return $this->getUser()->setAttribute('stats.filters', $filters, sfConfig::get('app_application_namespace'));
  }
  
  private function setLeagueOr404()
  {
    $idLeague = $this->getIdLeague();

    $this->league = LeagueQuery::create()->
                      findOneById($idLeague);

    $this->forward404If(!$this->league);
  }
  
  private function setFilterOrRedirect()
  {
    $this->filter = $this->getFilters();

    if(!isset($this->filter['id_league']) || !isset($this->filter['year']))
    {
      $this->redirect('@stats_start');
    };
  }
  
  private function getIdLeague()
  {
    $filter = $this->getFilters();
    
    return $filter['id_league'];
  }
  
  //TODO questa non serve più
  private function getIdLeagueAndSetAttribute(sfWebRequest $request)
  {
    $idLeague = $request->getParameter('idLeague', null);

    if(is_null($idLeague) && is_null($this->getIdLeagueAttribute()))
    {
      $this->forward404();
    }
    
    if(is_null($idLeague) || strlen($idLeague) == 0)
    {
      return $this->getIdLeagueAttribute();
    }
    else
    {
      $this->setIdLeagueAttribute($idLeague);
      return $idLeague;
    }
  }
  
  protected function getIdLeagueAttribute()
  {
    return $this->getUser()->getAttribute('selectedIdLeague',  null, sfConfig::get('application_namespace'));
  }
  
  protected function setIdLeagueAttribute($idLeague)
  {
    $this->getUser()->setAttribute('selectedIdLeague',  $idLeague, sfConfig::get('application_namespace'));
  }
  
  private function setPlayerOr404(sfWebRequest $request)
  {
    $username = $request->getParameter('username', null);

    $this->player = PlayerQuery::create()->
                    findOneByUsername($username);

    $this->forward404If(!$this->player);
  }

  //TODO questa è inutile
  private function getLeagues()
  {
    $this->leagues = LeagueQuery::create()->
                      find();
  }
  
  public function getLeaguesByLoggedUser()
  {
    $this->leagues = LeagueQuery::create()->
                     getByUserCriteria($this->getUser()->getGuardUser())->
                     find();
  }

  protected function buildCriteria()
  {
    $criteria = $this->filter->buildCriteria($this->getFilters());

    //$this->addSortCriteria($criteria);

    return $criteria;
  }
}
